<script type="text/javascript">
  var loaded = new Object;
  var next_dir = new Object;
% foreach my $k ( keys %next_dir ) {
  next_dir['<% $k %>'] = '<% $next_dir{$k} %>';
% }
</script>

% if ( $remote_error ) {
<div style="color: white; background: #804040; margin-top: 1em; margin-bottom: 1em">
              Could not read the data source (<% $TRFLP::dnacore_url %>): "<% $remote_error %>"
            </div>
% }
            <div id="container"></div>
            <form action="" onsubmit="return false">
              <fieldset>
                <label for="threshold">Noise threshold</label>
                <input type="text" id="threshold" value="2" size="3"></input>
                <label for="threshold">(x SD);</label>
                <label for="error"> &nbsp; Peak calling error</label>
                <input type="text" id="error" value="1" size="2"></input>
                <label for="error"> (bp)</label> &nbsp;             
                <input type="button" value="Calculate the Distance Matrix" onclick="load_distance_matrix();"></input>
                <input type="button" value="Hide" onclick="hide_distance_matrix();"></input>
                <hr></hr>
		<div id="matrix_container"></div>
                <hr></hr>
                <table cellspacing="0" cellpadding="2">
% my $dir_count = 3;
% foreach ( @dirs ) {
%#   last unless $dir_count--;
%   my $dirname = $_->[1];
%   my $directory = "$TRFLP::data_path/$dirname";
%   my $reject;
%   my $label;
%   my $dir_tag = "";
%   if ( -d $directory and grep /seq|ab1/, `ls $directory` ) {
%     $reject = 1;
%     $label = qq(<span style="text-decoration: line-through">$dirname</span>);
%   } else {
%     undef $reject;
%     $label = $dirname;
%     my $query = qq(SELECT "value" FROM batch_tag WHERE "set" = '$dirname' AND "key" = 'id');
%     ($dir_tag) = $Dbh->selectrow_array($query);
%     $Dbh->err and die $Dbh->errstr . " in query: [$query]";
%   }
%   if ( -d $directory ) {
%     my $stage1 = Util::stage1(`ls $directory`);
%     my $stage2 = Util::stage2(`ls $directory`);
                  <tr id="header-<% $dirname %>">
                    <td valign="top">
%     if ( $reject ) { # non-trflp data
                      &nbsp;
%     } else {
                      <a href="javascript:toggle_samples('<% $directory %>', '<% $dirname %>')"><img id="toggle-<% $dirname %>" src="lib/graphics/expand.png" style="border-width: 0" class="expand" alt=""></img></a>
%     }
                    </td>
                    <td valign="middle" style="white-space: nowrap">
%     if ( $reject ) { # non-trflp data
                      &nbsp;
%     } else {
                      <input type="checkbox" id="checkbox-<% $dirname %>" onchange="toggle_checkboxes(this)"></input>
%     }
                      <label for="select-<% $dirname %>"><% $label %></label>
                    </td>
                    <td valign="middle">
                      <input type="text" id="batch:<% $dirname %>" style="height: 1.5em; width: 15em" onkeypress="handle_key(this, event)" value="<% $dir_tag %>"></input>
                    </td>
                    <td valign="middle">
                      <input type="button" value="View" onclick="launch_viewer('<% $dirname %>', 'raw')"></input>
                    </td>
                    <td valign="middle">
%       if ( exists $ARGS{delete} ) {
                      <input type="image" src="lib/graphics/delete.gif" name="action" value="Erase:<% $dirname %>" style="vertical-align: center"></input>
%       } else {
                      &nbsp;
%       }
                    </td>
                    <td>
%     unless ( $reject ) { # trflp data
                      <table id="pipeline-expanded-<% $dirname %>" style="display: none">
                        <& lib/pipeline-1.mason, dir => $dirname, stage => $stage1 &>
                        <& lib/pipeline-2.mason, dir => $dirname, stage => $stage2 &>
                      </table>
                      <span id="pipeline-contracted-<% $dirname %>">
                        <& lib/pipeline-1-indicator.mason, stage => $stage1 &>
                      </span>
%     }
                    </td>
                  </tr>
%     unless ( $reject ) { # trflp data
                  <!-- data to be loaded dynamically from batch_job.html(directory => '<% $directory %>', dirname => '<% $dirname %>') -->
%     }
%   } else { # directory doesn't exist
                  <tr id="header-<% $dirname %>">
                    <td>&nbsp;</td>
                    <td style="white-space: nowrap"><% $label %></td>
                    <td>&nbsp;</td>
                    <td colspan="3">
                      <input type="button" name="copy" id="copy" value="Copy" onclick="launch_process('<% $dirname %>', 'copy')"></input>
                    </td>
                  </tr>
%   } # if -d $directory

% } # foreach remote directory
                </table>
              </fieldset>
            </form>
<%args>
  $action => undef;
  $dir => undef;
</%args>
<%init>
# perform the requested action
if ( $action ) {
  # --------------------------------------------
  if ( $action =~ /^Erase:(.+)$/ ) {
    my $dir = $1;
    die "null directory name" unless $dir;
    my $command = "rm -rf $TRFLP::data_path/$dir";
    print STDERR "running command: $command\n";
    die $! unless system($command) == 0;
  }

  # --------------------------------------------
  elsif ( $action eq 'Copy' ) {
    # This action doesn't get called anymore. It is performed by copy.cgi
  }
}
# ------------------------------------------------------------------------------------
# download the remote directory or list the local directory if remote is not available
my @dir = Util::list_remote_dirs();
my $remote_error;
if ( $dir[0] =~ /^ERROR/ ) {
  $remote_error = pop @dir;
  $remote_error =~ s/ERROR:\s*//;
}

my %seen;
foreach ( @dir ) {
  $seen{$_->[1]}++;
}
foreach (Util::list_local_dirs()) {
  push @dir, $_ unless $seen{$_->[1]}++;
}

#my @dirs = reverse sort {$a->[0] <=> $b->[0]} @dir; # by time stamp
my @dirs = reverse sort { # by name
  my @da = ($a->[1] =~ /^(\d\d)-(\d\d)-(\d\d)/);
  my @db = ($b->[1] =~ /^(\d\d)-(\d\d)-(\d\d)/);
  "$da[2]$da[0]$da[1]" <=> "$db[2]$db[0]$db[1]"
} @dir;

my %next_dir;
foreach my $i ( 0 .. $#dirs) {
  my $dirname = $dirs[$i]->[1];
  $next_dir{$dirname} = $i < $#dirs ? $dirs[$i+1]->[1] : 'none';
}
</%init>
<%method jshead>\
<script type="text/javascript" src="lib/index_handlers.js"></script>\
</%method>
